﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using sync_test.maps;
using nwrole.toolbox.sync;
using BLToolkit.Data.DataProvider;
using BLToolkit.Data;
using System.Data;
using System.Data.SqlClient;
using nwrole.toolbox.sync.contract;
using System.ServiceModel;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;

namespace sync_test
{

    public class PriceSync
    {

        #region bla

        private static T InitializeWS<T>()
        {
            EndpointAddress ep = new EndpointAddress("http://localhost:4781/TestService.svc");
            T proxy = ChannelFactory<T>.CreateChannel(new BasicHttpBinding(), ep);
            return proxy;
        }

        public void drawTextProgressBar(int progress, int total)
        {
            //draw empty progress bar
            Console.CursorLeft = 0;
            Console.Write("["); //start
            Console.CursorLeft = 32;
            Console.Write("]"); //end
            Console.CursorLeft = 1;
            float onechunk = 30.0f / total;

            //draw filled part
            int position = 1;
            for (int i = 0; i < onechunk * progress; i++)
            {
                Console.BackgroundColor = ConsoleColor.Gray;
                Console.CursorLeft = position++;
                Console.Write(" ");
            }

            //draw unfilled part
            for (int i = position; i <= 31; i++)
            {
                Console.BackgroundColor = ConsoleColor.Black;
                Console.CursorLeft = position++;
                Console.Write(" ");
            }

            //draw totals
            Console.CursorLeft = 35;
            Console.BackgroundColor = ConsoleColor.Black;
            Console.Write(progress.ToString() + " of " + total.ToString() + "    "); //blanks at the end remove any excess
        }

        #endregion

        public void OnPriceChanged(DataTable table)
        {
            SyncMessageEnvelope msg = new SyncMessageEnvelope(table.TableName, "ibbe3", table, Guid.NewGuid());
            var client = InitializeWS<ITestService>();
            client.ProcessMessage(msg);
        }

        public void OnPriceChanged(byte[] bytes, string tableName, string schema)
        {
            SyncMessageEnvelope msg = new SyncMessageEnvelope(tableName, "ibbe3", bytes, schema, Guid.NewGuid());
            var client = InitializeWS<ITestService>();
            client.ProcessMessage(msg);
        }

        //public void Run2()
        //{
        //    try
        //    {
        //        // setup sync
        //        GhostTableSync sync = GetGhostTableSync();
        //        sync.BatchSize = 1000;

        //        // run the sync
        //        sync.SyncWithReaderAndDirectUpdate(OBJEKTSBUNDET.Mapping, (bytes, tableName, schema) =>
        //        {
        //            Console.Write(".");
        //            OnPriceChanged(bytes, tableName, schema);
        //            return 1;
        //        });
        //    }
        //    catch (Exception e)
        //    {
        //        Console.WriteLine(e.ToString());
        //        int a = 1;
        //    }
        //}

     /*   private GhostTableSync GetGhostTableSync()
        {
            GhostTableSync sync = new GhostTableSync();
            sync.SourceConnectionString = SyncSettings.SrcConnectionString;
            sync.DestinationConnectionString = SyncSettings.DestConnectionString;
            sync.sourceDataProvider = new OdpDataProvider();
            sync.destinationDataProvider = new SqlDataProvider();
            sync.sqlBuilder = new SqlBuilder();
            return sync;
        } */
    }
 }




/*

        public void RunFullSync()
        {
            var map = new OBJEKTSBUNDET();
            try
            {
                // setup sync
                StagingSync sync = GetStagingSync();
                sync.BatchSize = 1000;

                // run the sync
                DbManager.DefaultConfiguration = ""; //to reset possible previous changes            
                using (DbManager db = new DbManager(sync.sourceDataProvider, sync.SourceConnectionString))
                {
                    db.SetCommand("select distinct OBJEKTID from wf.OBJEKTSBUNDET where rownum < 10");
                    var result = db.ExecuteDataTable();
                    foreach (DataRow row in result.Rows)
                    {
                        doPris(db, row);
                    }
                }

                // restore
                sync.RestoreMetadata(map);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
                int a = 1;
            }

        }


*/


/*


        public void RunFullSync2()
        {
            var map = new OBJEKTSBUNDET();
            try
            {

                BinaryFormatter serializationFormatter = new BinaryFormatter();
                MemoryStream buffer = new MemoryStream();

                object[] row = new object[3];

                // setup sync
                StagingSync sync = GetStagingSync();
                sync.BatchSize = 1000;

                // run the sync
                DbManager.DefaultConfiguration = ""; //to reset possible previous changes            
                using (DbManager db = new DbManager(sync.sourceDataProvider, sync.SourceConnectionString))
                {
                    db.SetCommand("select distinct OBJEKTID from wf.OBJEKTSBUNDET where rownum < 1000");
                    var reader = db.ExecuteReader();

                    int rowCounter = 0;
                    while (reader.NextResult())
                    {
                        rowCounter++;
                        for (int t = 0; t< reader.FieldCount; t++) {
                            row[t] = reader[t];
                        }
                        serializationFormatter.Serialize(buffer, row);
                        if (buffer.Length > 10000)
                        {
                            int a = 1;
                        }
                    }
                    //foreach (DataRow row in result.Rows)
                    //{
                    //    doPris(db, row);
                    //}
                }

                // restore
                sync.RestoreMetadata(map);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
                int a = 1;
            }

        }

*/


/*

        public void doPris(DbManager db, DataRow row)
        {
            Decimal? OBJEKTID = row["OBJEKTID"] as Decimal?;
            if (!objektList.Contains(OBJEKTID))
            {
                db.SetCommand("select * from wf.OBJEKTSBUNDET where OBJEKTID = :OBJEKTID",
                    db.Parameter(":OBJEKTID", OBJEKTID));
                DataTable table = db.ExecuteDataTable();
                table.TableName = "OBJEKTSBUNDET";
                OnPriceChanged(table);
                objektList.Add(OBJEKTID);
                System.Console.Write(".");
            }
            else
            {
                System.Console.Write("_");
            }

            drawTextProgressBar(totalCounter++, 12000);
        }


*/


/*


        public void Run()
        {
            var map = new OBJEKTSBUNDET();
            try
            {
                // setup sync
                StagingSync sync = GetStagingSync();
                sync.BatchSize = 1000;

                // run the sync
                sync.Sync(map, a =>
                {
                    DbManager.DefaultConfiguration = ""; //to reset possible previous changes            
                    using (DbManager db = new DbManager(sync.sourceDataProvider, sync.SourceConnectionString))
                    {
                        foreach (DataRow row in a.Rows)
                        {
                            doPris(db, row);
                        }
                    }
                    return 1;
                });
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
                int a = 1;
            }
        }

        List<decimal?> objektList = new List<decimal?>();
        int totalCounter = 0;
*/